package com.jjrwa.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiElement;
import com.jjrwa.transform.ServiceTransformHandler;
import org.jetbrains.annotations.NotNull;

public class SMethodAddController extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        try {
            ServiceTransformHandler.serviceMethodTransform(e);
        } catch (Exception exception) {
            exception.printStackTrace();
            Messages.showErrorDialog(exception.getMessage(), "File Transform Error");
        }
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        super.update(e);
        Editor editor = e.getData(PlatformDataKeys.EDITOR);
        e.getPresentation().setEnabledAndVisible(false);
        if (editor == null) {
            return;
        }
        PsiElement psiElement = e.getData(PlatformDataKeys.PSI_ELEMENT);
        if (psiElement == null) {
            return;
        }
        if (psiElement.toString().contains("PsiMethod:")) {
            e.getPresentation().setEnabledAndVisible(true);
        }
    }
}
